﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using EasyLucky.Common;
using EasyLucky.Dal.Dal;
using EasyLucky.Dal.Entity;
using EasyLucky.Manager.Models;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace EasyLucky.Manager.ViewModels
{
    public class UserManagerViewModel:ObservableObject
    {
        private UserManagerDataM dataM;

        public UserManagerDataM DataM
        {
            get { return dataM; }
            set { SetProperty(ref dataM, value); }
        }

        private UserDal userDal;


        public UserManagerViewModel()
        {
            this.DataM = new UserManagerDataM()
            {
                Users = new System.Collections.ObjectModel.ObservableCollection<Common.User>(),
                NewUser = new Common.User() { },
                NewVisibility = System.Windows.Visibility.Collapsed,
            };
        }

        private void InitInfo()
        {
            this.DataM.Users.Clear();
            var entities = this.userDal.QueryAll();
            foreach (var entity in entities)
            {
                var user = new User()
                {
                    Id = entity.Id,
                    Name = entity.Name,
                };
                this.DataM.Users.Add(user);
            }
        }

        #region Loaded

        private IRelayCommand loadedCommand;

        public IRelayCommand LoadedCommand
        {
            get { return loadedCommand ??= new RelayCommand(Loaded); }
        }

        private void Loaded()
        {
            if (userDal == null)
            {
                this.userDal = new UserDal();
                this.InitInfo();
            }
        }
        #endregion

        #region 查询

        private IRelayCommand queryCommand;

        public IRelayCommand QueryCommand
        {
            get { return queryCommand ??= new RelayCommand(Query); }
        }

        private void Query()
        {
            this.InitInfo();
        }

        #endregion

        #region 新增

        private IRelayCommand newCommand;

        public IRelayCommand NewCommand
        {
            get { return newCommand ??= new RelayCommand(New); }
        }

        private void New()
        {
            this.DataM.NewVisibility = System.Windows.Visibility.Visible;
            this.DataM.NewUser = new User();
        }

        private IRelayCommand saveCommand;

        public IRelayCommand SaveCommand
        {
            get { return saveCommand ??= new RelayCommand(Save); }
        }

        private void Save()
        {
            if (string.IsNullOrEmpty(this.DataM.NewUser.Name))
            {
                return;
            }
            var user = new UserEntity()
            {
                Id = this.DataM.NewUser.Id,
                Name = this.DataM.NewUser.Name
            };
            bool flag = false;
            if (user.Id > 0)
            {
                int cnt = this.userDal.UpdateUser(user);
                if (cnt > 0)
                {
                    flag = true;
                }
            }
            else
            {
                int id = this.userDal.AddUser(user);
                if (id > 0)
                {
                    flag = true;

                }
            }
            if (flag)
            {
                this.InitInfo();
            }
        }

        #endregion

        #region 编辑

        private IRelayCommand editCommand;

        public IRelayCommand EditCommand
        {
            get { return editCommand ??= new RelayCommand<User>(Edit); }
        }

        private void Edit(User user)
        {
            if (user == null)
            {
                return;
            }
            this.DataM.NewVisibility = Visibility.Visible;
            this.DataM.NewUser = user;
        }

        #endregion

        #region 删除

        private IRelayCommand delCommand;

        public IRelayCommand DelCommand
        {
            get { return delCommand ??= new RelayCommand<User>(Del); }
        }

        private void Del(User user)
        {
            if (user == null)
            {
                return;
            }
            if (user.Id < 1)
            {
                return;
            }
            var entity = new UserEntity()
            {
                Id = user.Id,
                Name = user.Name,
            };
            int cnt = this.userDal.DeleteUser(entity);
            if (cnt > 0)
            {
                InitInfo();
            }
        }

        #endregion
    }
}
